/*
 * Powered By dico
 * Since 2017 - 2018
 */
package com.seeyoui.kensite.bussiness.orderlang.orderProduct.controller;

import java.sql.*;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.seeyoui.kensite.common.util.DateUtils;
import com.seeyoui.kensite.common.util.Result;
import com.seeyoui.kensite.common.constants.StringConstant;
import com.seeyoui.kensite.common.base.domain.EasyUIDataGrid;
import com.seeyoui.kensite.common.base.controller.BaseController;
import com.seeyoui.kensite.common.util.RequestResponseUtil;
import com.seeyoui.kensite.common.util.excel.ExportExcel;

import org.apache.lucene.index.Term;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.seeyoui.kensite.common.base.domain.Page;
import com.seeyoui.kensite.framework.luence.domain.LuceneDocument;
import com.seeyoui.kensite.framework.luence.util.LuceneUtils;
import com.seeyoui.kensite.bussiness.orderlang.orderProduct.domain.OrderDetailForProductVO;
import com.seeyoui.kensite.bussiness.orderlang.orderProduct.domain.OrderProduct;
import com.seeyoui.kensite.bussiness.orderlang.orderProduct.domain.OrderProductVO;
import com.seeyoui.kensite.bussiness.orderlang.orderProduct.service.OrderProductService;
import com.seeyoui.kensite.bussiness.orderlang.productSpec.domain.ProductSpec;
import com.seeyoui.kensite.bussiness.orderlang.productSpec.service.ProductSpecService;
import com.seeyoui.kensite.bussiness.orderlang.warehouseProduct.service.WarehouseProductService;
/**
 * OrderProduct
 * @author dico
 * @version 2.0
 * @since 1.0
 * @date 2018-02-07
 */

@Controller
@RequestMapping(value = "orderlang/orderProduct")
public class OrderProductController extends BaseController {
	
	@Autowired
	private OrderProductService orderProductService;
	@Autowired
	private WarehouseProductService warehouseProductService;
	@Autowired
    private ProductSpecService productSpecService;
	
	/**
	 * 订单详情界面商品列表数据展示
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value = "/orderDetailForProduct", method=RequestMethod.POST)
    @ResponseBody
    public Result orderDetailForProduct(String orderId){
        Map map = new HashMap();
        map.put("orderId", orderId);
        List<OrderDetailForProductVO> productList = orderProductService.findOrderDetailForProduct(map);
        for (OrderDetailForProductVO orderDetailForProductVO : productList) {
            ProductSpec queryProductSpec= new ProductSpec();
            queryProductSpec.setProductPriceId(orderDetailForProductVO.getProductPriceId());
           List<ProductSpec> productSpecList =  productSpecService.findList(queryProductSpec);
           orderDetailForProductVO.setSepcList(productSpecList);
        }
        if(productList.size() > 0){
            return Result.success(productList);
        }else{
            return Result.failure("查询失败");
        }
    }
	
	/**
	 * 查询订单商品列表
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value = "/orderProductList", method=RequestMethod.POST)
	@ResponseBody
	public Result findProductListByOrderId(String orderId){
	    Map map = new HashMap();
	    map.put("orderId", orderId);
		List<OrderProductVO> productList = orderProductService.findProductListByOrderId(map);
		for (OrderProductVO orderProductVO : productList) {
		    ProductSpec queryProductSpec= new ProductSpec();
		    queryProductSpec.setProductPriceId(orderProductVO.getProductPriceId());
		   List<ProductSpec> productSpecList =  productSpecService.findList(queryProductSpec);
		   orderProductVO.setSepcList(productSpecList);
        }
		if(productList.size() > 0){
			return Result.success(productList);
		}else{
			return Result.failure("查询失败");
		}
	}
	
	/**
	 * 展示列表页面
	 * @param modelMap
	 * @param module
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("orderlang:orderProduct:view")
	@RequestMapping(value = "/{page}")
	public ModelAndView view(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, @PathVariable String page) throws Exception {
		return new ModelAndView("bussiness/orderlang/orderProduct/"+page, modelMap);
	}
	
	/**
	 * 根据ID查询单条数据
	 * @param modelMap
	 * @param module
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("orderlang:orderProduct:select")
	@RequestMapping(value = "/data/{id}")
	@ResponseBody
	public Object data(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, @PathVariable String id) throws Exception {
		OrderProduct orderProduct = orderProductService.findOne(id);
		return orderProduct;
	}
	
	/**
	 * 根据ID查询单条数据并返回相应表单
	 * @param modelMap
	 * @param module
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("orderlang:orderProduct:view")
	@RequestMapping(value = "/form/{page}/{id}")
	public ModelAndView form(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, @PathVariable String page, @PathVariable String id) throws Exception {
		OrderProduct orderProduct = orderProductService.findOne(id);
		modelMap.put("orderProduct", orderProduct);
		return new ModelAndView("bussiness/orderlang/orderProduct/"+page, modelMap);
	}
	
	/**
	 * 获取列表展示数据
	 * @param modelMap
	 * @param orderProduct
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("orderlang:orderProduct:select")
	@RequestMapping(value = "/list/data", method=RequestMethod.POST)
	@ResponseBody
	public Object listData(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, OrderProduct orderProduct) throws Exception {
		List<OrderProduct> orderProductList = orderProductService.findList(orderProduct);
		int total = orderProductService.findTotal(orderProduct);
		EasyUIDataGrid eudg = new EasyUIDataGrid();
		eudg.setTotal(String.valueOf(total));
		eudg.setRows(orderProductList);
		return eudg;
	}
	
	/**
	 * 获取列表展示数据带分页器用于jsp自己做分页
	 * @param modelMap
	 * @param orderProduct
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("orderlang:orderProduct:view")
	@RequestMapping(value = "/list/page", method=RequestMethod.POST)
	@ResponseBody
	public Object listPage(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, OrderProduct orderProduct) throws Exception {
		List<OrderProduct> orderProductList = orderProductService.findList(orderProduct);
		int total = orderProductService.findTotal(orderProduct);
		Page<OrderProduct> page = new Page<OrderProduct>(orderProduct.getPage(), orderProduct.getRows(), total);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("rows", orderProductList);
		dataMap.put("page", page);
		return dataMap;
	}
	
	/**
	 * 获取所有数据
	 * @param modelMap
	 * @param orderProduct
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("orderlang:orderProduct:select")
	@RequestMapping(value = "/list/all", method=RequestMethod.POST)
	@ResponseBody
	public Object listAll(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, OrderProduct orderProduct) throws Exception {
		List<OrderProduct> orderProductList = orderProductService.findAll(orderProduct);
		return orderProductList;
	}
	
	/**
	 * 保存新增的数据
	 * @param modelMap
	 * @param orderProduct
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("orderlang:orderProduct:insert")
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	@ResponseBody
	public String save(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, OrderProduct orderProduct) throws Exception {
		if (!beanValidator(modelMap, orderProduct)){
			RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
			return null;
		}
		orderProductService.save(orderProduct);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}
	
	/**
	 * 保存修改的数据
	 * @param modelMap
	 * @param orderProduct
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("orderlang:orderProduct:update")
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	@ResponseBody
	public String update(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, OrderProduct orderProduct) throws Exception {
		if (!beanValidator(modelMap, orderProduct)){
			RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
			return null;
		}
		orderProductService.update(orderProduct);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}
	
	/**
	 * 删除数据库
	 * @param modelMap
	 * @param orderProductId
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("orderlang:orderProduct:delete")
	@RequestMapping(value = "/delete", method=RequestMethod.POST)
	@ResponseBody
	public String delete(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, String id) throws Exception {
		List<String> listId = Arrays.asList(id.split(","));
		orderProductService.delete(listId);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}
	
	/**
	 * 假删除
	 * @param modelMap
	 * @param orderProduct
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("orderlang:orderProduct:delete")
	@RequestMapping(value = "/remove", method=RequestMethod.POST)
	@ResponseBody
	public String remove(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, OrderProduct orderProduct) throws Exception {
		orderProductService.remove(orderProduct);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}

	/**
	 * 导出Excel数据
	 * @param modelMap
	 * @param orderProduct
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("orderlang:orderProduct:export")
	@RequestMapping(value = "/export")
	public String export(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, OrderProduct orderProduct) throws Exception {
		String fileName = DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
		List<OrderProduct> orderProductList = orderProductService.findAll(orderProduct);
		new ExportExcel(null, OrderProduct.class).setDataList(orderProductList).write(response, fileName).dispose();
		return null;
	}
}